package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xiangyi.teachingsystem.entity.po.Course;
import com.xiangyi.teachingsystem.entity.po.CourseUser;
import com.xiangyi.teachingsystem.entity.po.GroupUser;
import com.xiangyi.teachingsystem.entity.po.User;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.CourseService;
import com.xiangyi.teachingsystem.service.CourseUserService;
import com.xiangyi.teachingsystem.service.GroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CourseUserController {
    @Autowired
    CourseUserService courseUserService;
    @Autowired
    CourseService courseService;

    @PostMapping("/addUsersToCourse")
    public Result addUsersToCourse(@RequestBody List<CourseUser> courseUsers){
        try {
            courseUserService.addUsersToCourse(courseUsers);
            return Result.success("用户批量添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("参数错误").setData(e.getMessage());
        }
    }

    @PostMapping("/removeUsersFromCourse")
    public Result removeUsersFromCourse(@RequestBody List<Integer> ids){
        try {
            courseUserService.removeByIds(ids);
            return Result.success("用户批量移除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/joinCourseUser")
    public Result joinCourseUser(@RequestParam Integer userId,
                             @RequestParam String sign){
        User user=courseService.getCourseUserBySign(sign);
        if (userId==user.getId())
            return Result.failure("无法加入自己开设的课程");

        CourseUser isExist = courseUserService.getOne(new QueryWrapper<CourseUser>()
                .eq("sign", sign)
                .eq("user_id", userId));
        if (isExist!=null)
            return Result.failure("课程已加入");

        try {
            return courseUserService.joinCourseUser(userId,sign);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/quitCourseUser")
    public Result quitCourseUser(@RequestParam Integer userId,
                                 @RequestParam String sign){
        try {
            courseUserService.remove(new QueryWrapper<CourseUser>()
                    .eq("user_id",userId)
                    .eq("sign",sign));
            return Result.success("退出课程成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/joinCourseList")
    public Result joinCourseList(@RequestParam Integer userId){
        try {
            List<Course> courseList= courseUserService.joinCourseList(userId);
            return Result.success("加入课程列表查询成功").setData(courseList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/usersList")
    public Result usersList(@RequestParam Integer courseId){
        try {
            List<CourseUser> courseUsers = courseUserService.usersList(courseId);
            return Result.success("课程学生列表查询成功").setData(courseUsers);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }
}
